package org.scrumpmt.web;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.scrumpmt.domain.Colaborador;
import org.scrumpmt.ejb.ColaboradorFacade;

/**
 *
 * @author aljiru
 */
public class ColaboradorConverter implements Converter {

    ColaboradorFacade colaboradorFacade = lookupColaboradorFacadeBean();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value.trim().equals("")) {
            return null;
        } else {
            return colaboradorFacade.find(new Long(value));
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((Colaborador) value).getId());
        }
    }

    private ColaboradorFacade lookupColaboradorFacadeBean() {
        try {
            Context c = new InitialContext();
            return (ColaboradorFacade) c.lookup("java:global/ScrumPMT/ScrumPMT-ejb/ColaboradorFacade!org.scrumpmt.ejb.ColaboradorFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
